linux 您所在的位置:网站首页 copy link location linux

linux

2023-12-08 18:28| 来源: 网络整理| 查看: 265

Most of the time, when I need to copy many symbolic links, I'm actually trying to mirror a directory tree. So I want the symlinks and everything else.

This is overkill for copying just a few symlinks, but if you're actually trying to copy an entire tree, this can be very useful:

Use tar.

user@host:/cwd$ ( cd /path/to/src ; tar cf - . ) | ( cd /path/to/dest ; tar xf - )

tar doesn't resolve the symlink by default, so symlinks in the mirror copy will point to the same locations as those in the original tree.

This trick makes use of subshells to get the tar command into position at the root of the directory to be mirrored; you can leave one of them out (along with the associated cd command) if you're already in the src or dest directories:

# already in src? user@host:/src$ tar cf - . | ( cd /path/to/dest ; tar xf - ) # already in dest? user@host:/dest$ ( cd /path/to/src ; tar cf - . ) | tar xf - # just need src/foo? # this result will be a mirror copy at dest/foo user@host:/src$ tar cf - foo | ( cd /path/to/dest ; tar xf - ) # mirror to another system? user@host:/src$ tar cf - . | ssh [email protected] '( cd /path/to/dest ; tar xf - )'

Again, this isn't appropriate for every time you want to copy symbolic links, but it is a very useful snippet to know.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有